package de.ifi.lmu.snitzelhunt.view;

import java.util.ArrayList;



import android.content.Intent;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.TextView;
import de.ifi.lmu.snitzelhunt.R;
import de.ifi.lmu.snitzelhunt.util.GSonUtility;
import de.ifi.lmu.snitzelhunt.util.basics.Ranking;

/**
 * The screen is the final screen that displays the users name and his rank in the ranking
 * 
 * @author Sarah Torma
 * 
 */
public class Activity_FinalScreen extends ActionBarActivity {

	String[] rankinListToString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity__final_screen);

		TextView text = (TextView) findViewById(R.id.TextViewRanking);

		RotateAnimation rotate = (RotateAnimation) AnimationUtils
				.loadAnimation(this, R.layout.rotate);
		text.setAnimation(rotate);

		Intent intent = getIntent();
		rankinListToString = intent.getStringArrayExtra("RANKING_LIST");
		String myRankingToString = intent.getStringExtra("MY_RANKING");
		
		System.out.println("MyRanking from participants: " + myRankingToString);

		//get Rankings from gson
		
		Ranking myRanking = new Ranking();
		myRanking = GSonUtility.deserializeRanking(myRankingToString);
		long rang = myRanking.getRanking();

		String name = myRanking.getRanking_name();

		ArrayList<Ranking> rankingList = new ArrayList<Ranking>();

		int index = 1;
		for (int i = 0; i < rankinListToString.length; i++) {
			rankingList.add(GSonUtility.deserializeRanking(rankinListToString[i]));
		}

		for (int i = 0; i < rankingList.size(); i++) {
			if (rankingList.get(i).getRanking_name().equals(name)
					&& rankingList.get(i).getRanking() == rang) {
				//+1 because list starts with 0
				index = i+1;
			}

		}
		int counterParticipants = rankinListToString.length;

	
		String message = "Hey " + name
				+ ", du hast die Route beendet und bist von "
				+ counterParticipants + " Teilnehmern auf Platz " + index;

		text.setText(message);

	}
	
	@Override
	public void onBackPressed() {
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity__final_screen, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void openRanking(View view) {
		Intent rankingintent = new Intent(this, Activity_Ranking.class);

		rankingintent.putExtra("RANKING_LIST", rankinListToString);

		startActivity(rankingintent);
	}
	
	/**
	 * If home button is clicked, kill intent and go to main screen
	 * @param view
	 */
	public void goHome(View view)
	{
	
			Intent returnToMainIntent = new Intent(this, MainActivity.class);
			returnToMainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(returnToMainIntent);
		
	}

}
